require(["common", "eventManager", "coAjax", "queryString", "href", "hash_routie", "jQuery.qrcode"], function(jSouper, eventManager, coAjax, QueryString, href, hash_routie) {
	hash_routie.on("goods_list", function() {
		var queryString = new QueryString(location.hash);
		var get_goods_type = queryString.get("id");
		var get_goods_tag = queryString.get("classify");
		App.set("$Cahce.goods_list_type", get_goods_type);
		App.set("thisPage", 1);
		var goods_pre_num = 12;

		// 加载标签列表
		// coAjax.get(appConfig.bus.tags_detail, {
		// 	bus_id: appConfig.bus_id
		// }, function(result) {
		// 	App.set("$Cache.goods_list.tags_detail", result.result);
		// });
		coAjax.get(appConfig.bus_classify_tree, {
			bus_id: appConfig.bus_id
		}, function(classify_tree) {
			console.log(classify_tree);
			App.set("$Cache.goods_list.classify_tree", classify_tree.result);
		});
		// 判断是否有子分类，展开子分类树
		App.set("$Event.goods_list.check_childs", function(e, vm) {
			console.log(vm.get("childs.length"))
			if (vm.get("childs.length") > 0) {
				App.set("$Cache.goods_list.childs_classify", vm.get("childs"));
				App.set("$Cache.goods_list.show_childs", true);
			} else {
				App.set("$Cache.goods_list.show_childs", false);
				App.set("$Cache.goods_list.childs_classify", []);
			};
		});
		// 关闭子分类树
		App.set("$Event.goods_list.close_childs", function() {
			App.set("$Cache.goods_list.show_childs", false);
		});
		//获取所有商品数量
		coAjax.get(appConfig.goods.number, {
			bus_id: appConfig.bus_id,
		}, function(result) {
			jSouper.ready(function() {
				var number = ~~result.result
				App.set("$Cache.goods_number", number);
				var _goods_number_list = [];
				_goods_number_list.length = number;
				var _page_num = [];
				//向上取整获取页数
				// console.log(number, goods_pre_num, Math.ceil(number / goods_pre_num));
				_page_num.length = Math.ceil(number / goods_pre_num);
				App.set("$Cache.goods_number_list", _goods_number_list);
				App.set("$Cache.page_num", _page_num);
			});
		});
		// 按分类加载商品
		function _init_classify_goods(classify_id) {
			App.set("goods", []);
			coAjax.get(appConfig.bus_classify_all_child_goods_by_id + classify_id, {
				bus_id: appConfig.bus_id
			}, function(classify_goods) {
				if (classify_goods.result.length) {
					App.set("goods", classify_goods.result);
				};
			});
			// 获取此分类信息
			coAjax.get(appConfig.bus_classify_by_id + classify_id, {
				bus_id: appConfig.bus_id
			}, function(classify_info) {
				App.set("$Cache.goods_list.list_title", classify_info.result.current_classify.classify_name);
			});
		};
		// 判断排序
		var current_page_num;
		if (get_goods_tag) {
			// 分类加载商品
			_init_classify_goods(get_goods_tag);
		} else {
			App.set("$Cache.goods_list.show_tags", false);
			if (get_goods_type == 2) {
				goods_list_url = appConfig.goods.quality_list;
				current_page_num = 0;
				App.set('cPrivate2', 'focus');
				App.set('cPrivate1', '');
				App.set("$Cache.goods_list.list_title", "销量最高");
				App.set('goods', []);
				getgoods();
			} else {
				var goods_list_url = appConfig.goods.list;
				current_page_num = 0;
				App.set('cPrivate2', '');
				App.set('cPrivate1', 'focus');
				App.set("$Cache.goods_list.list_title", "最新上架");
				App.set('goods', []);
				getgoods();
			};
		};
		//每页显示数量
		App.set("config.goods_pre_num", goods_pre_num);
		App.set("$Cache.goods_pre_num", goods_pre_num);
		jSouper.ready(function(argument) {
			App.set("$Cache.current_page_num", current_page_num);
		});
		App.set('$Event.getMoreGoods', function() {
			if (App.get('isresult')) {
				current_page_num += 1;
				getgoods();
			};
		});
		//获取指定页的商品
		function getgoods() {
			App.model.toggle('loadingoods');
			coAjax.get(goods_list_url, {
				bus_id: appConfig.bus_id,
				num: goods_pre_num,
				page: current_page_num
			}, function(result) {
				if (result.result.length) {
					App.set('isresult', true);
					App.concat('goods', result.result);
					App.model.toggle('loadingoods');
				} else {
					App.model.toggle('loadingoods');
					App.set('isresult', false);
				};
			});
		};

		// function _init_hash(tag_id, tags) {
		// 	var tags_detail = App.get("$Cache.goods_list.tags_detail");
		// 	console.log(tags_detail);
		// 	for (var i = 0; i < tags_detail.length; i++) {
		// 		if (tags_detail[i]._id == tag_id) {
		// 			App.set("goods", tags_detail[i].detail_content);
		// 			App.set("$Cache.goods_list.list_title", tags_detail[i].tag_name);
		// 			App.set("$Cache.goods_list.show_tags", false);
		// 			App.set("isresult", false);
		// 			break;
		// 		};
		// 	};
		// };
	});

	App.set("$Event.goods_list.show_tags", function() {
		App.set("$Cache.goods_list.show_childs", false);
		if (App.get("$Cache.goods_list.show_tags")) {
			App.set("$Cache.goods_list.show_tags", false);
			return;
		};
		App.set("$Cache.goods_list.show_tags", true);
	});
});